import {
  network
} from '../../utils/network.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total: '',
    comment: [],
    tags: [],
    item: {},
    type: '',
    id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let _this = this;
    let type = options.type;
    let id = options.id;
    _this.setData({
      type,
      id,
    })
    network.getItemDetail({
      type,
      id,
      success(res) {
        // 剧情类型是个数组,在页面上需要用/分开,所以这里要用join处理下,设置后再赋给res这样才能更新
        let genres = res.genres;
        // [1,2,1].join('/') = 1/2/3/
        genres = genres.join('/');
        res.genres = genres;
        // 处理过多的演员名单 
        let actors = res.actors;
        let actorsName = []
        if (actors.length > 3) {
          actors = actors.slice(0, 3)
        }
        for (let i = 0; i < actors.length; i++) {
          actorsName.push(actors[i].name)
        }
        // 演员用/分隔
        actorsName = actorsName.join('/')
        // 取导演
        let directors = res.directors[0].name;
        // 拼接导演和演员
        let authors = directors + "(导演) / " + actorsName;
        res.authors = authors;
        _this.setData({
          item: res,
        })
      },
    })
    // 请求标签接口
    network.getItemTags({
      type,
      id,
      success(res) {
        let tags = res.data.tags;
        _this.setData({
          tags,
        })
      }
    })
    // 请求短评
    network.movieComments({
      type,
      id,
      start: 0,
      count: 5,
      success(res) {
        let data = res.data;
        let total = data.total;
        let comment = data.interests;
        _this.setData({
          total,
          comment,
        })
        // console.log(data)
      }
    })
  },
  onShow(){
    wx.pageScrollTo({
      scrollTop: 0,
    })
  }
})